/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.pz.net.master;

import java.net.SocketAddress;

/**
 *
 * @author jannek
 */
public class Server extends Client {
    public String game;
    public int maxPlayers;
    public int playerCount;
    public byte[] data;
    public Client[] players;
    public Client[] connecting;

    public Server(String name, String game, short maxPlayers, byte[] data) {
        super(name);
        this.game = game;
        this.maxPlayers = maxPlayers;
        this.data = data;
        this.playerCount = 0;
        players = new Client[maxPlayers];
        connecting = new Client[maxPlayers];
    }

    public boolean connect(Client player) {
        if (playerCount <= maxPlayers)
            return false;
        for (int i = 0; i < maxPlayers; i++)
            if(connecting[i] == null) {
                connecting[i] = player;
                playerCount++;
                return true;
            }
        return false;
    }

    public void confirm(Client player) {
        for (int i = 0; i < maxPlayers; i++)
            if(connecting[i] == player) {
                connecting[i] = null;
                players[i] = player;
            }
    }

    public void kick(Client player) {
        for (int i = 0; i < maxPlayers; i++)
            if (players[i] == player) {
                players[i] = null;
                playerCount--;
            } else if (connecting[i] == player) {
                connecting[i] = null;
                playerCount--;
            }
    }
}
